package course.p4_list.s4_CycleList;

import course.p4_list.ListTest;
import org.junit.Test;

import static course.p4_list.util.Random.numberGenerator;

public class TestDemo implements ListTest {

    @Override
    @Test
    public void LengthTest() {
        Node cycleListNode= generatorCycleList(10,1,18);
        System.out.print("循环链表长度：");
        System.out.println(CycleList.ListLength(cycleListNode));
        CycleList.toString(cycleListNode);
    }

    @Override
    @Test
    public void InsertTest() {
        Node cycleListNode= generatorCycleList(10,1,18);
        System.out.print("循环链表长度：");
        System.out.println(CycleList.ListLength(cycleListNode));
        CycleList.toString(cycleListNode);
        System.out.println();
        System.out.println("插入操作：");
        Node test1 = new Node(23,null);
        cycleListNode= CycleList.Insert(cycleListNode,test1,9);
        CycleList.toString(cycleListNode);
        System.out.println();
    }

    @Override
    @Test
    public void DeleteTest() {
        Node cycleListNode= generatorCycleList(10,1,18);
        System.out.print("循环链表长度如下：");
        System.out.println(CycleList.ListLength(cycleListNode));
        CycleList.toString(cycleListNode);
        System.out.println();
        System.out.println("删除操作：");
        cycleListNode= CycleList.Delete(cycleListNode,10);
        CycleList.toString(cycleListNode);
    }

    /**
     * 自动随机创建循环链表，用于测试
     * @param length 链表长度
     * @param min 链表最小值
     * @param max 链表最大值
     * @return 生成的循环链表
     */
    public static Node generatorCycleList(int length, int min, int max) {
        Node headNode = null;
        Node nextNode = null;
        for(int i=0;i<length;i++){
            int num = numberGenerator(min,max);
            if(headNode==null){
                headNode = new Node(num,null);
                nextNode = headNode;
            }
            else {
                Node node = new Node(num,null);
                nextNode.next = (node);
                nextNode = node;
                if(i==length-1){
                    node.next = headNode;
                }
            }
        }
        return headNode;
    }



}
